package com.ayu.shortlink.settlement.service.handler.strategy;

import com.ayu.shortlink.settlement.common.enums.DiscountTypeEnum;
import com.ayu.shortlink.settlement.service.bases.strategy.AbstractDiscountStrategy;
import com.google.protobuf.ServiceException;
import lombok.SneakyThrows;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 按几折进行扣减策略
 */
@Component
public class DiscountCouponStrategy implements AbstractDiscountStrategy {
    @SneakyThrows
    @Override
    public BigDecimal handler(BigDecimal totalAmount, Map consumeRule) {
        Integer discount = (Integer)consumeRule.get("discount");
        if (discount == null || discount < 0 || discount > 100){
            throw new ServiceException("该优惠券折扣数额有误");
        }
        double compute = (double)discount/100;
        return totalAmount.multiply(BigDecimal.valueOf(compute));
    }

    @Override
    public Integer mark() {
        return DiscountTypeEnum.DISCOUNT_COUPON.getType();
    }
}
